package com.week.tiny_video.service.impl;

import com.week.tiny_video.dao.UserDao;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public boolean isExist(String username) {
        User user = getByName(username);
        return null != user;
    }

    @Override
    public User getByName(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public User get(String username, String password) {
        return userDao.getByUsernameAndPassword(username,password);
    }

    @Override
    public void add(User user) {
        userDao.save(user);
    }

    @Override
    public User getById(int id) {
        return userDao.findById(id).orElse(null);
    }

    @Override
    public List<User> list() {
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        return userDao.findAll(sort);
    }

    @Override
    public void update(User user) {
        userDao.save(user);
    }

    @Override
    public void delete(int id) {
        userDao.deleteById(id);
    }
}
